package com.nc.impl;

import java.rmi.RemoteException;
import java.util.HashMap;
import javax.xml.rpc.ServiceException;
import nc.bs.framework.common.NCLocator;
import nc.itf.uap.pf.IplatFormEntry;
import nc.vo.ic.pub.define.BillOperator;
import nc.vo.ic.pub.pf.ICPFParameter;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDouble;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.sf.allocate.AggAllocateVO;
import nc.vo.sf.allocate.AllocateBVO;
import nc.vo.sf.allocate.AllocateVO;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.nc.intf.IYanService;
import com.nc.tool.CheckJsonFormat;

public class YanServiceimpl implements IYanService {

	@Override
	public String query(String json) {
		JSONObject jso = new JSONObject();
		String status = null;
		CheckJsonFormat checkjson = new CheckJsonFormat();
		if (checkjson.validate(json)) {
			InsertImformation au = new InsertImformation();
			try {
				status = au.ff(json);
			} catch (RemoteException e) {
				e.printStackTrace();
			} catch (BusinessException e) {
				e.printStackTrace();
			} catch (ServiceException e) {
				e.printStackTrace();
			}
		} else {
			jso.put("statuscode", 1);
			jso.put("msg", "json格式不正确");
			return jso.toString();
		}
		return status;
	}
	
	/**
	 * 资金下拨单据生成接口
	 */
	@Override
	public String insertAllocateFromOA(String str) {
		
		boolean bool = isJson(str);
		if(bool==false)
		{
			JSONObject retjson = new JSONObject();
			retjson.put("statuscode", 1);
			retjson.put("msg", "json格式不正确");
			return retjson.toString();
		}
		
		//解析Json格式
		JSONObject json = JSONObject.fromObject(str);
		//表头数据
		AllocateVO hhh = new AllocateVO();
		hhh.setAttributeValue("vuserdef1", json.get("worlfowNum").toString());//填写OA流程号
		hhh.setAttributeValue("vuserdef2", json.get("typeByPay").toString());//填写OA的支付方式
		hhh.setAttributeValue("vuserdef3", json.get("countOfLine").toString());//填写OA总行数
		hhh.setMemo(json.get("memo").toString());
		
		JSONArray jsonb = json.getJSONArray("dataOfLine");//先将对象转成json数组
		for(int i=0;i<jsonb.size();i++)
		{
			String s1 = (String) jsonb.get(i);
			JSONObject js = JSONObject.fromObject(s1);
			
			AllocateBVO[] bbb = new AllocateBVO[1];
			bbb[0].setAttributeValue("vuserdef1", js.get("lineNum"));//OA的表体行号
			bbb[0].setRemark(js.get("remark").toString());//摘要
			bbb[0].setAmount(new UFDouble(js.get("amount").toString()));//下拨金额
			bbb[0].setPk_bankacc_p("1001A81000000010EFI0");//下拨银行账户 （来源视图的PK_BANKACCSUB）预置项
			bbb[0].setPk_org_r(js.get("ORG").toString());//收款单位（来源于试图的PK_ORG字段）
			bbb[0].setPk_bankacc_r(js.get("payee").toString());//收款单位银行账户PK（来源于试图的PK_BANKACCSUB字段）
			bbb[0].setPk_accid_r(js.get("bankOfDeposit").toString());//收款单位内部账户（来源于试图的PK_BANKACCBAS字段）
			String ret = saveAllocate(hhh, bbb);
		}
		
		//返回json给OA
		JSONObject retjson = new JSONObject();
		retjson.put("statuscode", 0);
		retjson.put("msg", "正确");
		return retjson.toString();
	}
	
	/**
	 * 调用保存脚本
	 * @param hhh
	 * @param bbb
	 * @return
	 */
	public String saveAllocate(AllocateVO hhh, AllocateBVO[] bbb)
	{
		IplatFormEntry service =  NCLocator.getInstance().lookup(IplatFormEntry.class);
		AggAllocateVO adjustVO = new AggAllocateVO();
		adjustVO.setParentVO(hhh);
		adjustVO.setChildrenVO(bbb);
		
		ICPFParameter para = new ICPFParameter();
		para.setBillaction(BillOperator.New);
		HashMap mm = new HashMap();
		mm.put("notechecked", "notechecked");
		try {
			Object ret = service.processAction("WRITE", "36K2", null, adjustVO, para, mm);
		} catch (BusinessException e) {
			ExceptionUtils.wrappBusinessException("调用资金下拨write脚本的时候异常"+e);
		}
		
		return null;
	}
	
	/**
	 * 检查是否是Json格式
	 * @param content
	 * @return
	 */
	public boolean isJson(String content){
	    try {
	    	JSONObject.fromObject(content);
	        return  true;
	   } catch (Exception e) {
	        return false;
	  }
	}

}
